$(document).ready(function(){
	$('#id_city').attr('onchange', 'checkCity()');
	
	initMap('add', 0, 0, 0, 0, 0);
	clickMap = false;
});

function checkCity(){
	$.ajax({
	   url: "checkCity/"+$('#id_city').val(),
	   success : function(data){
			readCitiesData(data);
	   }
	});	
}

function readCitiesData(data){
	var nodes   = data.getElementsByTagName("city");
	
	if (nodes.length==0){
		clickMap  = false;
		$('#city_selected').html("Aucune ville de notre base de donn&eacute;es ne correspond &agrave; " + $('#id_city').val() + ".");
	}
	else if (nodes.length==1){
		clickMap = false;
		$('#city_selected').html("La ville " + $('#id_city').val() + " a &eacute;t&eacute; s&eacute;lectionn&eacute;e.");
		$('#id_city_id').val(nodes[0].getAttribute("city_id"));
	}
	else {
		clickMap = true;
		$('#city_selected').html("Plusieurs villes correspondent &agrave; " + $('#id_city').val() + ". Situez la sur la carte.");

		var movies = "<movies>";
		// on ajoute un movie bidon pour qu'il soit jamais vide
		movies += "<movie title='dummy_movie' file='target' color='#ff0000' height='1' width='10' lat='-5000' long='50' fixed_size='true'></movie>"
		
		
		for (var i=0, c=nodes.length; i<c; i++) {
			movies += "<movie file='circle' color='#da3f26' height='8' width='8' lat='" + nodes[i].getAttribute("latitude") +"' long='" + nodes[i].getAttribute("longitude") + "' title='" + nodes[i].getAttribute("name") +"' oid='" +  nodes[i].getAttribute("city_id") + "' url='' fixed_size='true'></movie>";
		}
		
		movies += "</movies>";
		addDataToMap(movies);
	}
}